package cn.myfirefly.moming.common.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import cn.myfirefly.moming.common.model.StaffDto;
import cn.myfirefly.moming.common.service.DepartmentService;
import cn.myfirefly.moming.common.service.RoleDataAuthService;
import cn.myfirefly.moming.common.service.StaffService;
import cn.myfirefly.core.BaseController;
import cn.myfirefly.core.model.BaseDto;
import cn.myfirefly.core.model.BaseFilterDto;
import cn.myfirefly.core.model.DataAuth;
import cn.myfirefly.core.model.Identity;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.Permission;
import cn.myfirefly.utils.NetworkUtil;
import cn.myfirefly.utils.StrUtil;

public class BaseCommonController extends BaseController {
	
	@Autowired
	private RoleDataAuthService roleDataAuthService;
	
	@Autowired
	protected StaffService staffService;
	
	@Autowired
	private DepartmentService departmentService;
	
	@SuppressWarnings("unchecked")
	public DataAuth getDataAuth() {
		HttpServletRequest request = getRequest();
		Identity identity = (Identity)request.getSession().getAttribute("identity");
		Map<String, Permission> permissions = (Map<String, Permission>)request.getSession().getAttribute("permissions");
		if (!identity.isAdmin()) {
			String contextPath = request.getContextPath();
			String url = NetworkUtil.getRequestURL(request);
			if (!StrUtil.equals("/", contextPath)) {
				url = url.replaceFirst(contextPath, "");
			}
			int endIndex = url.indexOf("/", 1);
	        if (endIndex < 0) {
	        	endIndex = url.length();
			}
			String funId = url.substring(1, endIndex);
	        Permission permission = permissions.get(funId);
	        if (permission != null) {
				return permission.getDataAuth();
			}
		}
		return null;
	}
	
	public void wrapDataAuth(BaseFilterDto dto) {
		DataAuth dataAuth = getDataAuth();
		if (dataAuth != null) {
			dto.setDataAuth(getDataAuth());
			dto.setTargetDataAuthObject(getTargetDataAuthObject(dataAuth));
		}
	}
	
	public void wrapDataAuth(BaseDto dto) {
		DataAuth dataAuth = getDataAuth();
		if (dataAuth != null) {
			dto.setDataAuth(getDataAuth());
			dto.setTargetDataAuthObject(getTargetDataAuthObject(dataAuth));
		}
	}
	
	/**
	 * 根据数据权限信息，取得当前登录用户的数据权限对象的值
	 * @param dataAuth
	 * @return
	 */
	public Object getTargetDataAuthObject(DataAuth dataAuth) {
		if (StrUtil.equals(dataAuth.getTypeValue(), "person")) {
			// 如果数据权限类型是人员，则直接获取当前登录用户的ID或者用户名
			if (StrUtil.equals(dataAuth.getValueType(), "ID")) {
				return getLoginUser(getRequest()).getUserSn();
			} else if (StrUtil.equals(dataAuth.getValueType(), "NAME")) {
				return getLoginUser(getRequest()).getUserName();
			}
		} else if (StrUtil.equals(dataAuth.getTypeValue(), "departmemt")) {
			// 如果数据权限类型是部门，则直接取得当前登录用户所在的部门ID或者部门名称
			if (StrUtil.equals(dataAuth.getValueType(), "ID")) {
				return roleDataAuthService.getDeptIdByLoginName(getLoginUser(getRequest()).getLoginName());
			} else if (StrUtil.equals(dataAuth.getValueType(), "NAME")) {
				return roleDataAuthService.getDeptNameByLoginName(getLoginUser(getRequest()).getLoginName());
			}
		} else if (StrUtil.equals(dataAuth.getTypeValue(), "current_dept_person")) {
			// 如果数据权限类型是当前部门的人员，则直接取得当前登录用户所在的部门的所有人员ID或者名称
			String loginName = getLoginUser(getRequest()).getLoginName();
			Integer deptId = roleDataAuthService.getDeptIdByLoginName(loginName);
			if (StrUtil.equals(dataAuth.getValueType(), "ID")) {
				return roleDataAuthService.getUserIdByDeptId(deptId);
			} else if (StrUtil.equals(dataAuth.getValueType(), "NAME")) {
				return roleDataAuthService.getUserNameByDeptId(deptId);
			}
		} else if (StrUtil.equals(dataAuth.getTypeValue(), "dept_include_person")) {
			// 如果数据权限类型是部门及子部门人员，则直接取得当前登录用户所在部门以及子部门的所有人员ID或者名称
			// TODO 
		}
		return null;
	}
	
	public String departmentLookUp(HttpServletRequest request, 
			HttpServletResponse response) {
		String args_departmentSn = request.getParameter("args_departmentSn");
		if (StrUtil.isBlank(args_departmentSn)) {
			request.setAttribute("args_departmentSn", "departmentSn");
		} else {
			request.setAttribute("args_departmentSn", args_departmentSn);
		}
		String args_departmentName = request.getParameter("args_departmentName");
		if (StrUtil.isBlank(args_departmentName)) {
			request.setAttribute("args_departmentName", "departmentName");
		} else {
			request.setAttribute("args_departmentName", args_departmentName);
		}
		String args_departmentNo = request.getParameter("args_departmentNo");
		if (StrUtil.isBlank(args_departmentNo)) {
			request.setAttribute("args_departmentNo", "departmentNo");
		} else {
			request.setAttribute("args_departmentNo", args_departmentNo);
		}
		request.setAttribute("departments", departmentService.getAll());
		return "department/department-lookup";
	}
	
	public String staffLookup(String bizUrl, StaffDto dto,
			HttpServletRequest request, HttpServletResponse response) {
		String args_staffSn = request.getParameter("args_staffSn");
		if (StrUtil.isBlank(args_staffSn)) {
			request.setAttribute("args_staffSn", "staffSn");
		} else {
			request.setAttribute("args_staffSn", args_staffSn);
		}
		String args_staffName = request.getParameter("args_staffName");
		if (StrUtil.isBlank(args_staffName)) {
			request.setAttribute("args_staffName", "name");
		} else {
			request.setAttribute("args_staffName", args_staffName);
		}
		String staffName = request.getParameter("staffName");
		dto.setName(staffName);
		PageResultDto<StaffDto> resultDto = staffService.getList(dto);
		request.setAttribute("resultDto", resultDto);
		request.setAttribute("staffName", staffName);
		request.setAttribute("bizUrl", bizUrl);
		return "staff/staff-lookup";
	}
}
